package com.fengpanlong.web.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fengpanlong.entity.Employee;

public class UserCheckedFilter extends AbstractFilter {
	
	List<String> unCheckedURIList = new ArrayList<String>();
	String redURI;
	public void init(FilterConfig filterConfig) throws ServletException {
		String temp[] = filterConfig.getInitParameter("unChekcedURI").split(",");
		for (String s : temp) {
			unCheckedURIList.add("/" + s);
		}
		redURI = filterConfig.getInitParameter("redURI");
	}
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)resp;
		request.setCharacterEncoding("UTF-8");
		String uri = request.getRequestURI();
		String s = uri.substring(uri.lastIndexOf("/"));
		if(unCheckedURIList.contains(s)) {
			chain.doFilter(req, resp);
		} else {
			HttpSession session = request.getSession();
			Employee employee = (Employee) session.getAttribute("employee");
			if(employee == null) {
				String msg = "请先登录！";
				request.setAttribute("msg", msg);
				request.getRequestDispatcher(redURI).forward(request, response);
			} else {
				chain.doFilter(req, resp);
			}
		}
	}
}
